//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel.Design;
using Microsoft.Research.ScientificWorkflow.NetCDF.CSharpAPI;
using System.Workflow.ComponentModel.Compiler;

using Microsoft.Research.ScientificWorkflow;
using Microsoft.Research.ScientificWorkflow.Helpers;

using WorkflowData;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Hyper Cube Combine Transform")]
    [WorkflowCategory("NetCdf")]
    [Description("Combines out that data in two hypercubes along the record variable axis")]
    public class CombineHyperCubeActivity : Activity
	{
		public CombineHyperCubeActivity()
		{
			InitializeComponent();
		}

		#region Designer generated code

		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		[System.Diagnostics.DebuggerNonUserCode]
		private void InitializeComponent()
		{
			this.Name = "CombineHyperCube";
		}

		#endregion

		#region Dependency Properties

		public static DependencyProperty VariableNamesProperty = 
            DependencyProperty.Register("VariableNames", typeof(string), typeof(CombineHyperCubeActivity));

        [RequiredInputParam]
        [Name("Combine Variables", "Variables to be combined")]
        [Description("Name of the variables whose values need to be combined")]
		public string VariableNames
		{
			get
			{
				return ((string)(base.GetValue(CombineHyperCubeActivity.VariableNamesProperty)));
			}
			set
			{
				base.SetValue(CombineHyperCubeActivity.VariableNamesProperty, value);
			}
		}

        public static DependencyProperty FirstCubeProperty =
            DependencyProperty.Register("FirstCube", typeof(HyperCube), typeof(CombineHyperCubeActivity));

        [RequiredInputParam]
        [Name("First Hyper Cube")]
        [Description("First Hyper Cube for Merging")]
        public HyperCube FirstCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(CombineHyperCubeActivity.FirstCubeProperty)));
            }
            set
            {
                base.SetValue(CombineHyperCubeActivity.FirstCubeProperty, value);
            }
        }

        public static DependencyProperty SecondCubeProperty =
            DependencyProperty.Register("SecondCube", typeof(HyperCube), typeof(CombineHyperCubeActivity));

        [RequiredInputParam]
        [Name("Second Hyper Cube")]
        [Description("Second Hyper Cube for Merging")]
        public HyperCube SecondCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(CombineHyperCubeActivity.SecondCubeProperty)));
            }
            set
            {
                base.SetValue(CombineHyperCubeActivity.SecondCubeProperty, value);
            }
        }

        public static DependencyProperty CombinedCubeProperty =
            DependencyProperty.Register("CombinedCube", typeof(HyperCube), typeof(CombineHyperCubeActivity));

        [OutputParam]
        [Name("Resultant Hyper Cube")]
        [Description("Combined Hyper Cube containing merged data for specififed variable from both cubes.")]
        public HyperCube CombinedCube
        {
            get
            {
                return ((HyperCube)(base.GetValue(CombineHyperCubeActivity.CombinedCubeProperty)));
            }
            set
            {
                base.SetValue(CombineHyperCubeActivity.CombinedCubeProperty, value);
            }
        }

		#endregion

		protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
		{
            ParseVariables();

			IHyperCubeTransform transformer = new CombineHyperCubesTransform();
            this.CombinedCube = transformer.Transform(this.FirstCube, this.SecondCube, this.varNameArray);

			return ActivityExecutionStatus.Closed;
		}

        private string[] varNameArray;

        private void ParseVariables()
        {
            string[] v = (this.VariableNames).Split(new char[] { ',' });
            if (v.Length == 0)
                throw new ArgumentException("Error Converting from String to Variable Array");

            this.varNameArray = new string[v.Length];
            for (int i = 0; i < v.Length; i++)
            {
                this.varNameArray[i] = v[i];
            }
        }
    }
}
